package com.gxuwz.weatherapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class WeatherPagerAdapter extends RecyclerView.Adapter<WeatherPagerAdapter.WeatherViewHolder> {
    
    public static class WeatherData {
        String cityName;
        String currentTemp;
        String weatherInfo;
        String warningTitle;
        String warningContent;
        List<HourlyForecastAdapter.HourlyForecast> hourlyForecasts;
        List<DailyForecast> dailyForecasts;

        public WeatherData(String cityName, String currentTemp, String weatherInfo,
                          String warningTitle, String warningContent,
                          List<HourlyForecastAdapter.HourlyForecast> hourlyForecasts,
                          List<DailyForecast> dailyForecasts) {
            this.cityName = cityName;
            this.currentTemp = currentTemp;
            this.weatherInfo = weatherInfo;
            this.warningTitle = warningTitle;
            this.warningContent = warningContent;
            this.hourlyForecasts = hourlyForecasts;
            this.dailyForecasts = dailyForecasts;
        }
    }

    public static class DailyForecast {
        String date;
        String dayOfWeek;
        int weatherIconRes;
        String temperature;
        String probability;

        public DailyForecast(String date, String dayOfWeek, int weatherIconRes,
                            String temperature, String probability) {
            this.date = date;
            this.dayOfWeek = dayOfWeek;
            this.weatherIconRes = weatherIconRes;
            this.temperature = temperature;
            this.probability = probability;
        }
    }

    private final List<WeatherData> weatherDataList = new ArrayList<>();

    public void updateData(List<WeatherData> newWeatherData) {
        weatherDataList.clear();
        weatherDataList.addAll(newWeatherData);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public WeatherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.weather_page_item, parent, false);
        return new WeatherViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHolder holder, int position) {
        holder.bind(weatherDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return weatherDataList.size();
    }

    static class WeatherViewHolder extends RecyclerView.ViewHolder {
        private final TextView cityName;
        private final TextView currentTemp;
        private final TextView weatherInfo;
        private final TextView warningTitle;
        private final TextView warningContent;
        private final RecyclerView hourlyForecast;
        private final LinearLayout dailyForecast;
        private final HourlyForecastAdapter hourlyAdapter;

        public WeatherViewHolder(@NonNull View itemView) {
            super(itemView);
            cityName = itemView.findViewById(R.id.cityName);
            currentTemp = itemView.findViewById(R.id.currentTemp);
            weatherInfo = itemView.findViewById(R.id.weatherInfo);
            warningTitle = itemView.findViewById(R.id.warningTitle);
            warningContent = itemView.findViewById(R.id.warningContent);
            hourlyForecast = itemView.findViewById(R.id.hourlyForecast);
            dailyForecast = itemView.findViewById(R.id.dailyForecast);
            
            hourlyAdapter = new HourlyForecastAdapter();
            hourlyForecast.setAdapter(hourlyAdapter);
        }

        public void bind(WeatherData data) {
            cityName.setText(data.cityName);
            currentTemp.setText(data.currentTemp);
            weatherInfo.setText(data.weatherInfo);
            warningTitle.setText(data.warningTitle);
            warningContent.setText(data.warningContent);
            
            hourlyAdapter.updateData(data.hourlyForecasts);
            
            // 动态添加每日天气预报
            dailyForecast.removeAllViews();
            for (DailyForecast daily : data.dailyForecasts) {
                View dailyView = LayoutInflater.from(itemView.getContext())
                        .inflate(R.layout.daily_forecast_item, dailyForecast, false);
                
                TextView dateText = dailyView.findViewById(R.id.dateText);
                TextView dayText = dailyView.findViewById(R.id.dayText);
                ImageView weatherIcon = dailyView.findViewById(R.id.weatherIcon);
                TextView tempText = dailyView.findViewById(R.id.tempText);
                TextView probText = dailyView.findViewById(R.id.probabilityText);
                
                dateText.setText(daily.date);
                dayText.setText(daily.dayOfWeek);
                weatherIcon.setImageResource(daily.weatherIconRes);
                tempText.setText(daily.temperature);
                probText.setText(daily.probability);
                
                dailyForecast.addView(dailyView);
            }
        }
    }
} 